package gui.employee;

import gui.alert;
import gui.consumption.add_order;
import gui.manage.add_manage;
import gui.room.add_room;
import gui.user.add_user;
import pojo.Consumption;
import pojo.Guest;
import pojo.Guestroominfo;
import pojo.User;
import service.UserService;
import service.consumptionService;
import service.guestService;
import service.roomService;
import util.JTableDataUtil;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public class employee {
    private JPanel panel1;
    private JTabbedPane tabbedPane1;
    private JButton 客房信息录入Button;
    private JButton 客房信息查询Button;
    private JButton 客房预订Button;
    private JButton 入住登记Button;
    private JButton 添加房客Button;
    private JTextField user_id_input;
    private JButton 查询房客Button;
    private JTable guestroominfo_table1;
    private JButton fresh_room;
    private JTextField room_id_input;
    private JTable User_table1;
    private JButton 刷新Button;
    private JButton 按名字查询房客Button;
    private JButton del_user_Button;
    private JButton del_room_Button;
    private JTable manag_table1;
    private JTable comsu;
    private JTextField consu_id_input;
    private JButton consu_Button1;
    private JButton 离开登记Button;
    private JButton del_consu_Button;
    private JButton 增加员工Button;
    private JButton 删除员工Button;
    private JButton 刷新_em_Button1;
    private JTextField del_em_id;
    private JButton 设备入库Button;

    public employee() {
//        queryAllRooms();
        queryAllUsers();
        queryAllsu();
//        queryAllem();
//        客房信息录入Button.addMouseListener(new MouseAdapter() {
//            @Override
//            public void mouseClicked(MouseEvent e) {
//                super.mouseClicked(e);
//                add_room.main();
//            }
//        });
//        fresh_room.addMouseListener(new MouseAdapter() {
//            @Override
//            public void mouseClicked(MouseEvent e) {
//                super.mouseClicked(e);
//                queryAllRooms();
//            }
//        });
//        客房信息查询Button.addMouseListener(new MouseAdapter() {
//            @Override
//            public void mouseClicked(MouseEvent e) {
//                super.mouseClicked(e);
//                final String[] columnNames = {"序号", "类型", "租金", "状态"};
//                List<Guestroominfo> storeList = roomService.queryByid(Integer.parseInt(room_id_input.getText()));
//                JTableDataUtil<Guestroominfo> jTableDataUtil = new JTableDataUtil<>(Guestroominfo.class);
//
//                TableModel dataModel = new DefaultTableModel(jTableDataUtil.getObjData(storeList), columnNames);
//                guestroominfo_table1.setModel(dataModel);
//            }
//        });
        添加房客Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                add_user.show();
            }
        });
        刷新Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                queryAllUsers();
            }
        });
        查询房客Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                queryIdUsers();
            }
        });
        按名字查询房客Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                queryNameUsers();
            }
        });
        del_user_Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                guestService.deleteByid(Integer.parseInt(user_id_input.getText()));
                queryAllUsers();
            }
        });
//        del_room_Button.addMouseListener(new MouseAdapter() {
//            @Override
//            public void mouseClicked(MouseEvent e) {
//                super.mouseClicked(e);
//                roomService.deleteByid(Integer.parseInt(room_id_input.getText()));
//                queryAllRooms();
//            }
//        });
        客房预订Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                add_order.show();
            }
        });
        consu_Button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                queryAllsu();
            }
        });
        离开登记Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);

                List<Consumption> list = consumptionService.queryByid(Integer.parseInt(consu_id_input.getText()));
                Consumption consumption = list.get(0);
                consumption.setComment("已离开");

                if (consumptionService.updateByid(consumption) == 1) {
                    alert.show("离开成功");
                    List<Guest> list1 = guestService.queryByid((int) consumption.getGuestId());
                    Guest guest = list1.get(0);
                    guest.setIsInRoom("已离开");
                    guestService.updateByid(guest);
                    queryAllsu();
                } else {
                    alert.show("离开成功");
                }

            }
        });
        入住登记Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                List<Consumption> list = consumptionService.queryByid(Integer.parseInt(consu_id_input.getText()));
                Consumption consumption = list.get(0);
                consumption.setComment("已入住");

                if (consumptionService.updateByid(consumption) == 1) {
                    alert.show("入住成功");
                    List<Guest> list1 = guestService.queryByid((int) consumption.getGuestId());
                    Guest guest = list1.get(0);

//                    List<Guestroominfo> list2= roomService.queryByid((int)consumption.getRoomId());
//                    Guestroominfo guestroominfo = list2.get(0);
//                    guestroominfo.se
                    guest.setIsInRoom("已入住");
                    guestService.updateByid(guest);
                    queryAllsu();
                } else {
                    alert.show("失败成功");
                }
            }
        });
        del_consu_Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                consumptionService.deleteByid(Integer.parseInt(consu_id_input.getText()));
                queryAllsu();
            }
        });
//        刷新_em_Button1.addMouseListener(new MouseAdapter() {
//            @Override
//            public void mouseClicked(MouseEvent e) {
//                super.mouseClicked(e);
//                queryAllem();
//            }
//        });
//        增加员工Button.addMouseListener(new MouseAdapter() {
//            @Override
//            public void mouseClicked(MouseEvent e) {
//                super.mouseClicked(e);
//                add_manage.main();
//            }
//        });
//        删除员工Button.addMouseListener(new MouseAdapter() {
//            @Override
//            public void mouseClicked(MouseEvent e) {
//                super.mouseClicked(e);
//                UserService.deleteByid(Integer.parseInt(del_em_id.getText()));
//                queryAllem();
//            }
//        });
    }

//    public void queryAllRooms() {
//        final String[] columnNames = {"序号", "类型", "租金", "状态"};
//        List<Guestroominfo> storeList = roomService.all();
//        JTableDataUtil<Guestroominfo> jTableDataUtil = new JTableDataUtil<>(Guestroominfo.class);
//
//        TableModel dataModel = new DefaultTableModel(jTableDataUtil.getObjData(storeList), columnNames);
//        guestroominfo_table1.setModel(dataModel);
//    }

    public void queryAllUsers() {
        final String[] columnNames = {"序号", "姓名", "性别", "状态", "信息"};
        List<Guest> storeList = guestService.all();
        JTableDataUtil<Guest> jTableDataUtil = new JTableDataUtil<>(Guest.class);

        TableModel dataModel = new DefaultTableModel(jTableDataUtil.getObjData(storeList), columnNames);
        User_table1.setModel(dataModel);
    }

    public void queryIdUsers() {
        final String[] columnNames = {"序号", "姓名", "性别", "状态", "信息"};
        List<Guest> storeList = guestService.queryByid(Integer.parseInt(user_id_input.getText()));
        JTableDataUtil<Guest> jTableDataUtil = new JTableDataUtil<>(Guest.class);

        TableModel dataModel = new DefaultTableModel(jTableDataUtil.getObjData(storeList), columnNames);
        User_table1.setModel(dataModel);
    }

    public void queryNameUsers() {
        final String[] columnNames = {"序号", "姓名", "性别", "状态", "信息"};
        List<Guest> storeList = guestService.queryByName(user_id_input.getText());
        JTableDataUtil<Guest> jTableDataUtil = new JTableDataUtil<>(Guest.class);

        TableModel dataModel = new DefaultTableModel(jTableDataUtil.getObjData(storeList), columnNames);
        User_table1.setModel(dataModel);
    }

    public void queryAllsu() {
        final String[] columnNames = {"序号", "用户id", "姓名", "客房id", "客房类型", "费用", "开始时间",
                "结束时间", "天数", "信息"};
        List<Consumption> storeList = consumptionService.all();
        JTableDataUtil<Consumption> jTableDataUtil = new JTableDataUtil<>(Consumption.class);

        TableModel dataModel = new DefaultTableModel(jTableDataUtil.getObjData(storeList), columnNames);
        comsu.setModel(dataModel);
    }

//    public void queryAllem() {
//        final String[] columnNames = { "用户id", "密码", "等级"};
//        List<User> storeList = UserService.all_user();
//        JTableDataUtil<User> jTableDataUtil = new JTableDataUtil<>(User.class);
//
//        TableModel dataModel = new DefaultTableModel(jTableDataUtil.getObjData(storeList), columnNames);
//        manag_table1.setModel(dataModel);
//    }


    public void show() {
        JFrame frame = new JFrame("客房管理系统");
        frame.setContentPane(new employee().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
